package features.advance.leetcode;

import java.util.Stack;

/**
 * @author LIN
 * @date 2021-04-25
 */
public abstract class Solution {
    private static int a = 0;

    public void a(int[] arr,int pos){
        new StringBuilder();
    }

    /**
     * 用两个栈模拟一个队列
     * @param <T>
     */
    static class DefinedQueue<T>{
        Stack<T> s1 = new Stack<>();
        Stack<T> s2 = new Stack<>();

        public void add(T t){
            while(!s1.isEmpty()){
                s2.push(s1.pop());
            }
            s1.push(t);

            while(!s2.isEmpty()){
                s1.push(s2.pop());
            }
        }

        /**
         * 查看队首元素
         * @return T
         */
        public T peek(){
            if(s1.isEmpty()){
                return null;
            }
            return s1.peek();
        }

        /**
         * 弹出队首元素
         * @return T
         */
        public T poll(){
            if(s1.isEmpty()){
                return null;
            }
            return s1.pop();
        }

        public static void main(String[] args) {
            DefinedQueue<Integer> queue = new DefinedQueue<>();
            queue.add(1);
            queue.add(2);
            queue.add(3);
            System.out.println(queue.peek());// 1
            System.out.println(queue.peek());// 1
            System.out.println(queue.poll());// 1
            System.out.println(queue.peek());// 2
            System.out.println(queue.poll());// 2
            System.out.println(queue.poll());// 3
            System.out.println(queue.peek());// null
            System.out.println(queue.poll());// null

            System.out.println(queue.binarization(100));


        }

        public int binarization(int num){
            int c = 1;
            while(num > c){
                System.out.print(c+",");
                num -= c;
                c *=2;
            }
            return num;
        }
    }
}

